﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace ProjectM2Work
{
    public class TrucXanhScreen : GameScreen
    {
        private Texture2D _background;
        private List<Button> _listMainButton;
        private List<Button> _listCardButton;
        private List<Texture2D> _listTextureTrucXanh;
        private List<int> _listIndexForCheck;
        private Button lblLevel, lblScore;
        private int _preIndexCardChoose, _preInput;
        private int _inputgo1, _inputgo2;
        private bool _isDisappear2Card;
        private int _countFull;
        private int _level;
        private int _score;
#region  Initilation
        public TrucXanhScreen(GamePage maingame,SpriteBatch spriteBatch, Texture2D background,
            List<Texture2D> listTextureMainButton, List<Texture2D> listTextureTrucXanh, SpriteFont miniMenuFont)
            : base (maingame, spriteBatch)
        {
            _spriteBatch = spriteBatch;
            _background = background;
            // Main button gom 3 nut home, about, exit
            _listMainButton = new List<Button>();
            //_listMainButton.Add(new Button(listTextureMainButton[1], new Vector2(70, 395)));
            //_listMainButton.Add(new Button(listTextureMainButton[2], new Vector2(30, 450)));

            for (int i = 0; i < _listMainButton.Count; i++)
            {
                _listMainButton[i].Selected += new Button.IsSelected(ButtonInGameMenuScreen_Selected);
            }

            //LblLevel
            lblLevel = new Button("Level: ", miniMenuFont, new Vector2(100, 150)) { HaveAnimation = false };
            lblScore = new Button("Level: ", miniMenuFont, new Vector2(80, 200)) { HaveAnimation = false, ColorText = Color.Red };

            //Init cho card
            _listTextureTrucXanh = listTextureTrucXanh;
            InitCardToPlay(_listTextureTrucXanh);
        }

        void ButtonInGameMenuScreen_Selected(int input)
        {
            switch (input)
            {
                //case 0: _maingame.GoToThongTin(); break;    // thong tin
                //case 1: _maingame.ShowScreen(0);  break;    // thoat
            }
        }

#endregion

#region Methods
        #region Update & Draw
        public override void OnUpdate(GameTimerEventArgs gameTime)
        {
            lblLevel.Text = "Cap do: " + _level.ToString("00");
            lblScore.Text = "Diem: " + _score.ToString();
            if (_isDisappear2Card)
            {
                _listCardButton[_inputgo1].Position = new Vector2(_listCardButton[_inputgo1].Position.X - 20, _listCardButton[_inputgo1].Position.Y);
                _listCardButton[_inputgo2].Position = new Vector2(_listCardButton[_inputgo2].Position.X - 20, _listCardButton[_inputgo2].Position.Y);
                if (_listCardButton[_inputgo1].Position.X < -150 && _listCardButton[_inputgo2].Position.X < -150)
                    _isDisappear2Card = false;
            }
            if (this.Enabled)
            {
                base.OnUpdate(gameTime);
                while (TouchPanel.IsGestureAvailable)
                {
                    GestureSample gesture = TouchPanel.ReadGesture();
                    switch (gesture.GestureType)
                    {
                        case GestureType.Tap:
                            {
                                //for (int i = 0; i < _listMainButton.Count; i++)
                                    //_listMainButton[i].ButtonIsTouched(gesture.Position, i);
                                for (int i = 0; i < _listCardButton.Count; i++)
                                    _listCardButton[i].ButtonIsTouched(gesture.Position, i);
                            }
                            break;
                        case GestureType.FreeDrag:
                            {
                            }
                            break;
                        case GestureType.DoubleTap:
                            {
                            }
                            break;
                        case GestureType.Pinch:
                            {
                            }
                            break;
                        case GestureType.DragComplete:
                            {
                            }
                            break;
                        default:
                            {

                            }
                            break;
                    }
                }
            }            
        }

        public override void OnDraw(GameTimerEventArgs gameTime)
        {
            if (this.Visible)
            {
                base.OnDraw(gameTime);
                _spriteBatch.Begin();
                _spriteBatch.Draw(_background, Vector2.Zero, Microsoft.Xna.Framework.Color.White);
                //for (int i = 0; i < _listMainButton.Count; i++)
                    //_listMainButton[i].Draw(_spriteBatch, gameTime);
                for (int i = 0; i < _listCardButton.Count; i++)
                    _listCardButton[i].Draw(_spriteBatch, gameTime);
                lblLevel.Draw(_spriteBatch, gameTime);
                lblScore.Draw(_spriteBatch, gameTime);
                _spriteBatch.End();
            }
        }

        public void InitCardToPlay(List<Texture2D> listTextureTrucXanh)
        {
            int countPairCard = 9;
            _listIndexForCheck = new List<int>();
            _listCardButton = new List<Button>();

        #region  Init _listIndexForCheck
            for (int i = 0; i < (countPairCard * 2); i++)
                _listIndexForCheck.Add(-1);
            for (int i = 0; i < countPairCard; i++)
            {
                int indexCard = -1;
                bool ok = false;
                while (!ok)
                {
                    indexCard = Everywhere.rand.Next(1, 26);
                    ok = true;
                    for (int j = 0; j < (countPairCard * 2); j++)
                        if (_listIndexForCheck[j] == indexCard)
                        {
                            ok = false;
                            break;
                        }
                }
                for (int j = 0; j < (countPairCard * 2); j++)
                    if (_listIndexForCheck[j] == -1)
                    {
                        _listIndexForCheck[j] = indexCard;
                        break;
                    }
                int indexOfIndexCard = -1;
                ok = false;
                while (!ok)
                {
                    ok = true;
                    indexOfIndexCard = Everywhere.rand.Next(1, (countPairCard * 2));
                    if (_listIndexForCheck[indexOfIndexCard] != -1)
                        ok = false;
                }

                _listIndexForCheck[indexOfIndexCard] = indexCard;
            }
        #endregion

        #region Init _listCardButton;
            int Yposition = 200;
            int indexrow = 0;
            for (int i = 0; i < (countPairCard * 2); i++)
            {
                _listCardButton.Add(new Button(listTextureTrucXanh[0], new Vector2(indexrow*80 + 250,Yposition)));
                indexrow++;
                if (indexrow != 0 && indexrow % 6 == 0)
                {
                    Yposition += 100;
                    indexrow = 0;
                }
                _listCardButton[i].Selected += new Button.IsSelected(CardButtonTrucXanh_Selected);
            }
        #endregion

            _preIndexCardChoose = -1;
            _isDisappear2Card = false;
            _countFull = 0;
            _level = 1;
        }

        void CardButtonTrucXanh_Selected(int input)
        {            
            if (_preIndexCardChoose == -1)
            {
//                _listCardButton[input].Texture = _listTextureTrucXanh[_listIndexForCheck[input]];
                _preIndexCardChoose = _listIndexForCheck[input];
                _preInput = input;
            }
            else
            {
                if (_preIndexCardChoose == _listIndexForCheck[input])
                {
                    if (_preInput != input)
                    {
                        _listCardButton[input].Texture = _listTextureTrucXanh[_listIndexForCheck[input]];
                        _preIndexCardChoose = -1;
                        Disappear2Card(input, _preInput);
                    }                    
                }
                else
                {
                    for (int i=0;i<_listIndexForCheck.Count;i++)
                            _listCardButton[i].Texture = _listTextureTrucXanh[0];
                    _preIndexCardChoose = _listIndexForCheck[input];
                    _preInput = input;
                }
            }
            if (_preIndexCardChoose != -1)
                _listCardButton[input].Texture = _listTextureTrucXanh[_listIndexForCheck[input]];
        }

        private void Disappear2Card(int input, int preInput)
        {
            _isDisappear2Card = true;
            _inputgo1 = input;
            _inputgo2 = preInput;
            _countFull++;
            _score += 10;
            if (_countFull == 9)
            {
                InitCardToPlay(_listTextureTrucXanh);
                _level++;
            }
        }

        #endregion
    }
#endregion
}
